/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "gmock/gmock-actions.h"
#include "mockcpp/mockcpp.hpp"
#include "manager/CloudServiceManager.h"
#include "common/CommonStruct.h"
#include "ObsPlugin.h"
#include "Path.h"

using namespace std;
using namespace Module;

namespace {
constexpr auto MP_SUCCESS = 0;
constexpr auto INTERNAL_ERROR = 200;
}  // namespace

class ObsPluginTest : public testing::Test {
public:
    void SetUp() {};
    void TearDown()
    {
        GlobalMockObject::verify();
    };
};

/*
 * 用例名称：AppInit
 * 前置条件：无
 * check点：日志log配置初始化
 */
TEST_F(ObsPluginTest, AppInit)
{

    string logPath = Module::CPath::GetInstance().GetRootPath() + "/log";
    EXPECT_EQ(AppInit(logPath), Module::SUCCESS);

    string emptyPath = "";
    EXPECT_EQ(AppInit(emptyPath), Module::SUCCESS);
}

/*
 * 用例名称：CheckApplication
 * 前置条件：无
 * check点：检查请求，函数体为空
 */
TEST_F(ObsPluginTest, CheckApplication)
{

    ActionResult returnValue;
    ApplicationEnvironment appEnv;
    Application application;
    CheckApplication(returnValue, appEnv, application);

    EXPECT_EQ(0, 0);
}

/*
 * 用例名称：ListApplicationResourceV2
 * 前置条件：无
 * check点：列出请求资源，函数体为空
 */
TEST_F(ObsPluginTest, ListApplicationResourceV2)
{

    ResourceResultByPage returnValue;
    ListResourceRequest request;
    ListApplicationResourceV2(returnValue, request);

    EXPECT_EQ(request.applications.empty(), true);
}

TEST_F(ObsPluginTest, ListApplicationResource)
{
    std::vector<ApplicationResource> returnValue;
    ApplicationEnvironment appEnv;
    Application application;
    ApplicationResource parentResource;
    ListApplicationResource(returnValue, appEnv, application, parentResource);

    EXPECT_EQ(0, 0);
}

/*
* 用例名称：AllowRestoreInLocalNode
* 前置条件：无
* check点：函数体为空
*/
TEST_F(ObsPluginTest, AllowRestoreInLocalNode)
{

    ActionResult returnValue;
    const AppProtect::RestoreJob job;

    EXPECT_NO_THROW(AllowRestoreInLocalNode(returnValue, job));
}

/*
* 用例名称：AllowRestoreSubJobInLocalNode
* 前置条件：无
* check点：函数体为空
*/
TEST_F(ObsPluginTest, AllowRestoreSubJobInLocalNode)
{

    ActionResult returnValue;
    const AppProtect::RestoreJob job;
    const AppProtect::SubJob subJob;

    EXPECT_NO_THROW(AllowRestoreSubJobInLocalNode(returnValue, job, subJob));
}

/*
* 用例名称：AllowRestoreSubJobInLocalNode
* 前置条件：无
* check点：函数体为空
*/
TEST_F(ObsPluginTest, AllowBackupInLocalNode)
{

    ActionResult returnValue;
    AppProtect::BackupJob job;
    AppProtect::BackupLimit::type limit;

    AllowBackupInLocalNode(returnValue, job, limit);

    EXPECT_EQ(returnValue.code, 0);
}

/*
 * 用例名称：AllowBackupSubJobInLocalNodeSuccess
 * 前置条件：无
 * check点：允许备份子任务在本节点执行
 */
TEST_F(ObsPluginTest, AllowBackupSubJobInLocalNodeSuccess)
{
    Module::StorageVerifyInfo verifyInfo;
    Module::HWCloudService HwCloudService(verifyInfo, obs_auth_switch::OBS_OBS_TYPE);
    Module::OBSResult ObsResult;
    ObsResult.result = Module::ResultType::SUCCESS;
    MOCKER_CPP_VIRTUAL(HwCloudService, &Module::HWCloudService::CheckConnect)
        .stubs()
        .will(returnValue(ObsResult));

    ActionResult returnValue;
    AppProtect::BackupJob job;
    OBSPlugin::ObjectStorageAuthExtendInfo authInfo;
    authInfo.storageType = "2";
    std::string authInfoStr;
    ASSERT_EQ(Module::JsonHelper::StructToJsonString(authInfo, authInfoStr), true);
    job.protectEnv.auth.extendInfo = authInfoStr;
    AppProtect::SubJob subJob;

    AllowBackupSubJobInLocalNode(returnValue, job, subJob);

    EXPECT_EQ(returnValue.code, 0);
}

/*
 * 用例名称：AllowBackupSubJobInLocalNodeFailed
 * 前置条件：无
 * check点：允许备份子任务在本节点执行
 */
TEST_F(ObsPluginTest, AllowBackupSubJobInLocalNodeFailed)
{
    Module::StorageVerifyInfo verifyInfo;
    Module::HWCloudService HwCloudService(verifyInfo, obs_auth_switch::OBS_OBS_TYPE);
    Module::OBSResult ObsResult;
    ObsResult.result = Module::ResultType::FAILED;
    MOCKER_CPP_VIRTUAL(HwCloudService, &Module::HWCloudService::CheckConnect)
        .stubs()
        .will(returnValue(ObsResult));

    ActionResult returnValue;
    AppProtect::BackupJob job;
    OBSPlugin::ObjectStorageAuthExtendInfo authInfo;
    authInfo.storageType = "2";
    std::string authInfoStr;
    ASSERT_EQ(Module::JsonHelper::StructToJsonString(authInfo, authInfoStr), true);
    job.protectEnv.auth.extendInfo = authInfoStr;
    AppProtect::SubJob subJob;

    AllowBackupSubJobInLocalNode(returnValue, job, subJob);

    EXPECT_EQ(returnValue.code, INTERNAL_ERROR);
}
